
/**
 * Created by Administrator on 2016/12/5.
 */
! function() {
  $(function() {
    
    function getFormField(formId) {
      var form = $(formId);
      var result = {};
      var field = form.find('[name]');
      field.each(function(index, item) {
        var $item = $(item);
        result[$item.attr('name')] = $item.val();
      })
      return result;
    }
    layui.use(['upload', 'form'], function() {
      var upload = layui.upload;
      var form = layui.form;
      var layer = layui.layer;
      //执行实例
      var uploadInst = upload.render({
        elem: '#test1', //绑定元素
        data: {
          type: 1
        },
        url: '/cloudFileServer/sso/ssoServiceSystem/uploadPicture', //上传接口
        before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
            $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
          });
        },
        done: function(res) {
          if (res.code === 0) {
            $('#test1').html('<img width="100%" height="100%" src="/cloudFileServer/sso/ssoServiceSystem/getImage?fileName=' + res.data.systemImage + '"></img>')
            $('#systemImage').val(res.data.systemImage)
          } else {
            layer.msg(res.msg);
          }
          //上传完毕回调
        },
        error: function() {
          layer.msg('服务器错误!上传失败')
        }
      });
      $('#addSystem').on('click', function() {

        $('#system').modal('show');
        $('#addSystemForm').bootstrapValidator({
          feedbackIcons: {
  　　　　   valid: 'glyphicon glyphicon-ok',
  　　　　　 invalid: 'glyphicon glyphicon-remove',
          },
          fields: {
            name: {
              message: '用户名验证失败',
              validators: {
                notEmpty: {
                  message: '请输入业务系统名称'
                }
              }
            },
            remark: {
              validators: {
                notEmpty: {
                    message: '请输入业务系统描述'
                }
              }
            }
          }
        }).on('success.form.bv', function(e) {
          // Prevent form submission
          e.preventDefault();
          $.ajax({
            url: '/sso/ssoServiceSystem/add',
            type: 'post',
            data: getFormField('#addSystemForm'),
            error: function(err) {layer.msg('服务器错误!新增失败')},
            success: function(result) {
              if (result.code === '0') {
                layer.msg('新增成功');
                setTimeout(function() {
                  location.reload();
                }, 300)
              } else if (data.code === '-1'){
                layer.msg('登录超时,即将跳转登录页')
                location.href = '/login';
              } else {
                layer.msg(result.msg);
              }
            }
          })
        })
      })
    })
    $('body').on('click', '[data-system-del]', function() {
      var systemInfo = {
        name: $(this).data('systemName'),
        systemCode: $(this).data('systemDel'),
      };
      deleteSystem(systemInfo);
    })
    $('.system-item').on('mouseleave', function() {
      $(this).find('.system_tools').addClass('hide');
    })
    $('.title').on('dblclick', function() {
      $(this).parents('.system-item').find('.system_tools').removeClass('hide');
    })
    // 删除账户
    function deleteSystem(systemInfo) {
      var index = layer.confirm('确定要删除 ' + systemInfo.name, {
        btn: ['确定', '取消'] //按钮
      }, function() {
        $.ajax({
          url: '/cloud/kcloudBusinessSystem/delete',
          type: "POST",
          dataType: "json",
          data: {
            systemCode: systemInfo.systemCode
          },
          error: function() {}
        }).done(function(data) {
          if (data.code) {
            if (data.code === '0') {
              layer.msg('删除成功');
              location.reload();
            } else if (data.code === '-1'){
              layer.msg('登录超时,即将跳转登录页')
              location.href = '/login';
            } else {
              layer.msg(data.msg);
            }
          } else {
            layer.msg('服务器错误');
          }
        })
      }, function() {
        layer.close(index);
      })
    }
  });
}();
